Solutions for Comp248 Assignment 3

//This program calculates the quotient Q and the 
//Remainder R without using "/"
// or the "%" mathematical operator, it is 
//merely done using the relationship:
// M=QxN+R, where Q=Q+1 and R=R-N.

#include<iostream.h>
#include "stdlib.h"

int main()

{

//All objects in this assignment have to be integers.
int M,N,Q,R; 




//The program will always enter this loop since the Variable Count
//has always a value of 1, so it will always read what's within
//braces until a -ve M or a -ve / zero N is input.
//This is the primary loop.

	
//the loop will always be entered until some condition along the 
//way is violated!
//Furthermore, this very loop can also be implemented using the do 
//while(condition)because it enters the loop then it checks 
//for conditions and this is needed here!

while (true)
{
//Prompting the values for M & N then reading it respectively:
		cout<<endl;			
		cout<<"__________________________"<<endl;
		cout<<endl;
		cout<<" Enter the number M: ";
		cin>>M;
		cout<<" Enter the number N: ";
		cin>>N;
		cout<<"__________________________"<<endl;
		cout<<endl;
			
	
//Program doesn't work if the numerator is -ve or the denominator is zero.
//the program exits the primary loop if this condition is violated, thru.
//the exit function from the stdlib header file, (0) is normal, (1) is
//abnornal so it exits.

				if (M<0 || N<=0) 
					{
cout<<" Error terminated the program because: "<<endl<<endl;
					if (M<0)
							{
		cout<<" - M is Negative!!"<<endl;
							}
						
		if(N<0)
							{
		cout<<" - N is Negative!!"<<endl;
							} 
						
		if(N==0)
							{
		cout<<" - N is Zero!!"<<endl;
							} 

					cout<<endl;
					exit(1); 	

					}
				
				else
					{
//Initializing the values for the Quotient and the Remainder.	
						Q=0;
						R=M;
		
//Formatting the Quotient/Remainder Table
cout<<" Quotient "<<'\t'<<" Remainder "<<endl;
cout<<" ________ "<<'\t'<<" _________ "<<endl<<endl;
			
		 
//This is a nested loop
while  (R>=N)					
{
	Q+=1;
	R=R-N;			
	M=Q*N+R; 		
//cout<<"   "<<Q<<'\t'<<'\t'<<"   "<<R<<endl<<endl;						
						
}

cout<<" M/N : "<<Q <<'\t'<<" M%N : "<<R<<endl;
cout<<"__________________________"<<endl;
cout<<endl;	
				}
		}		 

	
	return 0;  
}


//Run:
/*
__________________________

 Enter the number M: 123
 Enter the number N: 30
__________________________

 Quotient        Remainder
 ________        _________

 M/N : 4         M%N : 3
__________________________


__________________________

 Enter the number M: 12
 Enter the number N: 3
__________________________

 Quotient        Remainder
 ________        _________

 M/N : 4         M%N : 0
__________________________


__________________________

 Enter the number M: 35
 Enter the number N: 7
__________________________

 Quotient        Remainder
 ________        _________

 M/N : 5         M%N : 0
__________________________


__________________________

 Enter the number M: 10
 Enter the number N: 0
__________________________

 Error terminated the program because:

 - N is Zero!!

Press any key to continue


  
__________________________

 Enter the number M: -10
 Enter the number N: 1
__________________________

 Error terminated the program because:

 - M is Negative!!

Press any key to continue
  
	
__________________________

 Enter the number M: 100
 Enter the number N: -50
__________________________

 Error terminated the program because:

 - N is Negative!!

Press any key to continue


__________________________

 Enter the number M: -100
 Enter the number N: 0
__________________________

 Error terminated the program because:

 - M is Negative!!
 - N is Zero!!

Press any key to continue


__________________________

 Enter the number M: -100
 Enter the number N: -2
__________________________

 Error terminated the program because:

 - M is Negative!!
 - N is Negative!!

Press any key to continue
						   
*/